package com.eqdd.yiqidian.ui.mine.setting;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.allenliu.versionchecklib.core.AllenChecker;
import com.allenliu.versionchecklib.core.VersionParams;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.XiTongSettingActivityCustom;
import com.eqdd.yiqidian.adapter.SectionDividerItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.EnterBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.AppUtils;
import com.eqdd.yiqidian.utils.LogoutUtil;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.web.JustWebActivity;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.EditDialogFragment;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.suke.widget.SwitchButton;

import java.util.ArrayList;
import java.util.List;

public class XiTongSettingActivity extends BaseActivity {
    private XiTongSettingActivityCustom dataBinding;

    private Class[] classes = new Class[]{
            null,
            null,
            FeedbackActivity.class,
            JustWebActivity.class,
            null
    };
    public List<Object> enterBeans = new ArrayList<>();

    private AlertDialog alertTipDialog;
    private SlimAdapterEx slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_xi_tong_setting);
        initTopTitleBar(View.VISIBLE, "系统设置");
    }

    @Override
    public void setView() {

        enterBeans.add(new EnterBean("消息提示音").setSwitch(true).setEnter(false));
        enterBeans.add(new EnterBean("通知").setSwitch(true).setEnter(false));
        enterBeans.add(new EnterBean("帮助与反馈"));
        enterBeans.add(new EnterBean("关于易企点"));
        enterBeans.add(new EnterBean("当前版本").setRight(AppUtils.getLocalVersionName(XiTongSettingActivity.this)));
        enterBeans.add(new EnterBean("退出登陆").setEnter(false));
        slimAdapterEx.updateData(enterBeans);
    }

    @Override
    public void initData() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this, 2, 3, 1, 1).setLastBootomHeght(0));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.item_enter, new SlimInjector<EnterBean>() {
            @Override
            public void onInject(EnterBean data, IViewInjector injector) {
                injector.text(R.id.tv_content, data.getContent())
                        .visibility(R.id.iv_enter, data.isEnter() ? View.VISIBLE : View.INVISIBLE)
                        .visibility(R.id.switch_bt, data.isSwitch() ? View.VISIBLE : View.INVISIBLE);
                if (!TextUtils.isEmpty(data.getRight())) {
                    injector.text(R.id.tv_right, data.getRight())
                            .visible(R.id.tv_right);
                } else {
                    injector.invisible(R.id.tv_right);
                }
                if (data.isSwitch()) {
                    if (slimAdapterEx.getData().indexOf(data) == 0) {
                        ((SwitchButton) injector.findViewById(R.id.switch_bt)).setChecked((Boolean) SPUtil.getParam(AppConstent.isRongTipOn, true));
                        ((SwitchButton) injector.findViewById(R.id.switch_bt)).setOnCheckedChangeListener((view, isChecked) -> {
                            SPUtil.setParam(AppConstent.isRongTipOn, isChecked);
                        });
                    } else {
                        ((SwitchButton) injector.findViewById(R.id.switch_bt)).setChecked((Boolean) SPUtil.getParam(AppConstent.isRongNotifyOn, true));

                        ((SwitchButton) injector.findViewById(R.id.switch_bt)).setOnCheckedChangeListener((view, isChecked) -> SPUtil.setParam(AppConstent.isRongNotifyOn, isChecked));
                    }
                }
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());

        ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener(
                (rv, p, v) -> {
                    switch (p) {
                        case 2:
                        case 3:
                            startActivity(classes[p]);
                            break;
                        case 4:
                            OkGo.<HttpResult<VersionInfo>>post(HttpConfig.BASE_URL + HttpConfig.LATEST_VERSION)
                                    .params("type", "安卓")
                                    .params("version", AppUtils.getLocalVersionName(XiTongSettingActivity.this))
                                    .execute(new DialogCallBack<HttpResult<VersionInfo>>(XiTongSettingActivity.this) {
                                        @Override
                                        public void onSuccess(Response<HttpResult<VersionInfo>> response) {
                                            HttpResult<VersionInfo> httpResult = response.body();
                                            if (httpResult.getStatus() == 200) {

                                                showDialog(httpResult.getItems());
                                            } else if (httpResult.getStatus() == 300) {
                                                ToastUtil.showShort("当前版本已是最新版本");
                                            }
                                        }
                                    });
                            break;
                        case 5:
                            if (alertTipDialog == null) {
                                alertTipDialog = ChooseAlertView.getAlertTipDialog(XiTongSettingActivity.this, "提醒", "你确认要退出登录吗", new EditDialogFragment.OnClickListener() {
                                    @Override
                                    public void onClick(List<String> contents) {
                                        LogoutUtil.logout();
                                    }
                                });
                            }
                            alertTipDialog.show();
                            break;
                    }

                }
        );
    }

    private void showDialog(VersionInfo versionInfo) {
//        new MaterialDialog.Builder(this)
//                .title("新版本发布了")
//                .content("我们优化了界面与算法")
//                .iconRes(R.mipmap.eqd_logo)
//                .positiveText("下载新版本")
//                .negativeText("取消")
//                //点击事件添加 方式1
//                .onAny((dialog, which) -> {
//                    if (which == DialogAction.POSITIVE) {
//
//                    }
//
//                })
//                .show();
        //如果仅使用下载功能，downloadUrl是必须的
        VersionParams.Builder builder = new VersionParams.Builder();
        builder.setOnlyDownload(true)
                .setDownloadUrl(versionInfo.getUrl())
                .setTitle("新版本发布了")
                .setUpdateMsg(versionInfo.getRemark());

        AllenChecker.startVersionCheck(this, builder.build());
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    class VersionInfo {

        /**
         * id : 2
         * type : 安卓
         * version : v7.11.4
         * url : 116.196.113.145:8090/eqdd.apk
         * createTime : 2017-11-06T10:53:29.843
         */

        private int id;
        private String type;
        private String version;
        private String url;
        private String createTime;
        private String remark;

        public String getRemark() {
            return remark;
        }

        public VersionInfo setRemark(String remark) {
            this.remark = remark;
            return this;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getCreateTime() {
            return createTime;
        }

        public void setCreateTime(String createTime) {
            this.createTime = createTime;
        }
    }
}
